using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PerlinNoise
{
    public partial class SummedPerlinNoiseOptions : Form
    {
        public SummedPerlinNoiseOptions(uint octavesNumber, double persistence)
        {
            InitializeComponent();

            this.octavesNumberText.Text = octavesNumber.ToString();
            this.persistenceText.Text = persistence.ToString();

            this.okButton.Focus();
        }

        public uint OctavesNumber
        {
            get { return uint.Parse(this.octavesNumberText.Text); }
        }

        public double Persistence
        {
            get { return double.Parse(this.persistenceText.Text); }
        }

        public UnproperControl? areParametersProper()
        {
            UnproperControl? unproperControl = null;

            try {
                uint octavesNumber = uint.Parse(this.octavesNumberText.Text);
                if (octavesNumber <= 0) {
                    throw new FormatException();
                }
            } catch (FormatException) {
                unproperControl = new UnproperControl(this.octavesNumberText,
                    "Octaves number should be integer value greater than 0");
            }

            try {
                double.Parse(this.persistenceText.Text);
            } catch (FormatException) {
                unproperControl = new UnproperControl(this.persistenceText,
                    "Persistence value should be floating point number");
            }

            return unproperControl;
        }

        private void okButton_Click(object sender, EventArgs evt)
        {
            UnproperControl? unproperControl = this.areParametersProper();

            if (unproperControl == null) {
                this.DialogResult = DialogResult.OK;
            } else {
                MessageBox.Show(this, unproperControl.Value.msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                unproperControl.Value.control.Focus();
            }
        }

        private void cancelButton_Click(object sender, EventArgs evt)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}